package com.article.controller;

import com.article.entity.dto.SearchNoticeTypeDao;
import com.article.entity.dto.UpdateNoticeTypeDao;
import com.article.entity.pojo.ArticleType;
import com.article.entity.pojo.NoticeType;
import com.article.entity.vo.SearchNoticeTypeVo;
import com.article.result.Result;
import com.article.service.NoticeTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/noticeType")
public class NoticeTypeController {

    @Autowired
    private NoticeTypeService noticeTypeService;

    @GetMapping("/{page}/{limit}")
    public Result getNoticeType(@PathVariable("page") int page, @PathVariable("limit") int limit) {
        log.info("查询文章类型，页码：{}，每页显示条数：{}", page, limit);
        List<NoticeType> result = noticeTypeService.getArticleType(page, limit);
        return Result.success(result);
    }

    @GetMapping("/total")
    public Result getNoticeTypeTotal() {
        log.info("查询文章类型总数");
        int total = noticeTypeService.selectNoticeTypeNumber();
        return Result.success(total);
    }

    @PostMapping("/search")
    public Result searchNoticeType(@RequestBody SearchNoticeTypeDao searchNoticeTypeDao) {
        log.info("查修条件为:{}", searchNoticeTypeDao);
        List<NoticeType> result = noticeTypeService.searchNoticeType(searchNoticeTypeDao);
        int total = noticeTypeService.selectNoticeTypeNumber(searchNoticeTypeDao);
        SearchNoticeTypeVo searchNoticeTypeVo = SearchNoticeTypeVo.builder().total(total).noticeTypeList(result).build();
        return Result.success(searchNoticeTypeVo);
    }

    @PostMapping("/add")
    public Result addNoticeType(@RequestBody NoticeType noticeType) {
        log.info("添加文章类型:{}", noticeType);
        int result = noticeTypeService.addNoticeType(noticeType);
        if (result == 1) {
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @PostMapping("/update")
    public Result updateNoticeType(@RequestBody NoticeType noticeType) {
        log.info("更新文章类型:{}", noticeType);
        int result = noticeTypeService.updateNoticeType(noticeType);
        if (result == 1) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteNoticeType(@PathVariable("id") int id) {
        log.info("删除文章类型，id:{}", id);
        int result = noticeTypeService.deleteNoticeType(id);
        if (result == 1) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @PostMapping("/deletes")
    public Result deleteNoticeTypes(@RequestBody List<Integer> ids) {
        log.info("批量删除文章类型，ids:{}", ids);
        int result = noticeTypeService.deleteNoticeTypes(ids);
        if (result > 0) {
            return Result.success("删除成功");
        }

        return Result.error("删除失败");
    }

    @PutMapping("/updateStatus")
    public Result updateNoticeTypeStatus(@RequestBody UpdateNoticeTypeDao updateNoticeTypeDao) {
        log.info("更新文章类型状态，id:{},status:{}", updateNoticeTypeDao.getId(), updateNoticeTypeDao.getStatus());
        int result = noticeTypeService.updateNoticeTypeStatus(updateNoticeTypeDao.getId(), updateNoticeTypeDao.getStatus());
        if (result == 1) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }
}
